[์๋ฐ ์น ํ๋ก๊ทธ๋๋ฐ Next Step - ๋ฐ์ฌ์ฑ]์ ์ฝ๊ณ ์ธ์ฉํ๊ณ ์ ๋ฆฌํ๋ POST์ ๋๋ค.
"์๋ก์ด ์ ํ๋ฆฌ์ผ์ด์ ์ ๊ฐ๋ฐํ๋ ์์ ์ ์ฝ์ง ์์ ์์ ์ด๋ค. ํ์ง๋ง ๊ทธ๋ณด๋ค ๋ ์ด๋ ค์ด ์์ ์ ๋๊ตฐ๊ฐ ๊ฐ๋ฐํด๋์ ์ ํ๋ฆฌ์ผ์ด์ ์ ์ง์์ ์ผ๋ก ๊ธฐ๋ฅ ์ถ๊ฐ, ๊ฐ์ ์ ํ๋ฉด์ ๊ธฐ์ ์ ์ผ๋ก ๊ณ์ํด์ ๋ฐ์ ์์ผ ๋๊ฐ๋ ์์ ์ด๋ค."
์ง๊ธ๊น์ง ๊ตฌํํ MVC ํ๋ ์์ํฌ์ ๋ฌธ์ ์ ์ ํ์ ํ ํ ์๋ก์ด MVC ํ๋ ์์ํฌ๋ฅผ ๊ตฌํํ๋ค. ์ด์ ์ ๊ตฌํํ MVC ํ๋ ์์ํฌ ๊ธฐ๋ฐ์ผ๋ก ๋์ํ๋ ์ปจํธ๋กค๋ฌ๊ฐ ์ ์์ ์ผ๋ก ๋์ํ๋ ์ํ์์ ์๋ก์ด MVC ํ๋ ์์ํฌ๋ก ์ ์ง์ ์ผ๋ก ์ ํํด๊ฐ๋ ๊ณผ์ ์ ๋ํด ์ดํด๋ณธ๋ค.
๐ฉ MVC ํ๋ ์์ํฌ ์๊ตฌ์ฌํญ 3๋จ๊ณ
์๋ฐ์ ๋ฆฌํ๋ ์ API์ ๋ํ ํ์ฉ๊ณผ ๊ตฌํํด์ผ ํ ํด๋์ค๊ฐ ๋ง์์ ธ ๋์ด๋๊ฐ ๋์์ง๋ค.
๐ง ์๊ตฌ์ฌํญ
9์ฅ ์ค๊ฐ ์ ๊ฒ์ ๋ง์ง๋ง ๋ฌธ์ ๋ฅผ ๋ณด๋ฉด, ์๋ก์ด ์ปจํธ๋กค๋ฌ ์ถ๊ฐ๋ง๋ค ๋งค๋ฒ RequestMapping ํด๋์ค์ ์์ ์ฌํญ์ด ๋ฐ์ํ๋ ๊ฒ์ ๋ํ ๊ท์ฐฎ์์ ์ ๊ธฐํ๋ค. ์ ์ง๋ณด์ ์ฐจ์์์ ์ปจํธ๋กค๋ฌ์ ์๊ฐ ๊ณ์ ์ฆ๊ฐํ๊ณ ์๊ณ , ๊ฐ ์ปจํธ๋กค๋ฌ์ execute() ๋ฉ์๋๋ฅผ ๋ณด๋ฉด, 10์ค ์ด์์ธ ๊ฒฝ์ฐ๋ ๊ฑฐ์ ์๋ค.
" ๋ฐ๋ผ์, ์๋ก์ด ๊ธฐ๋ฅ์ด ์ถ๊ฐ๋ ๋๋ง๋ค ๋งค๋ฒ ์ปจํธ๋กค๋ฌ๋ฅผ ์ถ๊ฐํ๋ ๊ฒ์ด ์๋ ๋ฉ์๋๋ฅผ ์ถ๊ฐํ๋ ๋ฐฉ์์ด๋ฉด ๋ ํจ์จ์ ์ผ ๋ฏํ๋ค. "
๋ ํ ๊ฐ์ง ์์ฌ์ด ์ ์ ์์ฒญ URL ๋งคํ ์, HTTP ๋ฉ์๋ ์ ๋ณด๋ ํจ๊ป ๋งคํ๋๋ฉด ์ข์ ๊ฒ ๊ฐ๋ค. URL์ ๊ฐ์ง๋ง, ๋ค๋ฅธ ๊ธฐ๋ฅ ์ํ์ ์ํด ๋ค๋ฅธ ๋ฉ์๋๋ก ๋งคํํ๋ ๊ฒ ๋ํ ๊ฐ๋ฅํ๋ฉด ์ข์ ๊ฒ ๊ฐ๋ค.
@Controller
public class MyController {
private static final Logger log = LoggerFactory.getLogger(MyController.class);
@RequestMapping("/users")
public ModelAndView list(HttpServletRequest req, HttpServletResponse res) {
log.debug("users findUserId");
return new ModelAndView(new JspView("/users/list.jsp"));
}
@RequestMapping(value = "/users/show", method = RequestMethod.GET)
public ModelAndView show(HttpServletRequest req, HttpServletResponse res) {
log.debug("users findUserId");
return new ModelAndView(new JspView("/users/show.jsp"));
}
@RequestMapping(value = "/users", method = RequestMethod.POST)
public ModelAndView create(HttpServletRequest req, HttpServletResponse res) {
log.debug("users create");
return new ModelAndView(new JspView("redirect:/users"));
}
}
-
์ ๊ธฐ๋ฅ์ ์ง์ํ๋ annotation ๊ธฐ๋ฐ์ ์๋ก์ด MVC ํ๋ ์์ํฌ๋ฅผ ๊ตฌํํด์ผ ํ๋ค.
- ์ค์ต์ ์ํด ๋ผ๋๊ฐ ๋๋ ์ฝ๋์ ํ ์คํธ ์ฝ๋๊ฐ ์๊ณ , ํด๋น ํ ์คํธ ํด๋์ค์ 3๊ฐ์ ํ ์คํธ ๋ฉ์๋๋ค์ด ์ฑ๊ณตํ๋ฉด ๊ตฌํ์ด ์๋ฃ๋ ๊ฒ์ด๋ค.
์๋ก์ด MVC ํ๋ ์์ํฌ๋ฅผ ์ถ๊ฐํ์ผ๋ ์ด์ ์ ๊ตฌํํ ์ปจํธ๋กค๋ฌ๋ฅผ annotation ๊ธฐ๋ฐ์ผ๋ก ๋ณ๊ฒฝํด์ผ ํ๋ค.
-
ํ์ง๋ง ์ ํ๋ ์์ํฌ ์ ์ฉ์ ์ํด ํ ๋ฒ์ ๋ชจ๋ ์ปจํธ๋กค๋ฌ๋ฅผ ๋ณ๊ฒฝํ๊ฒ ๋๋ฉด, ์ผ์ ๊ธฐ๊ฐ ๋์ ์๋ก์ด ๊ธฐ๋ฅ์ ์ถ๊ฐํ๊ฑฐ๋ ๋ณ๊ฒฝํ๋ ์์ ์ ์ค๋จํด์ผ ํ๋ค.
- ์ด๋ฅผ ๋ณด์ํ๊ธฐ ์ํด ์ ์ง์ ๋ฆฌํฉํ ๋ง์ด ๊ฐ๋ฅํ ๊ตฌ์กฐ๋ก ๊ฐ๋ฐํด์ผ ํ๋ค.
์ ์ง์ ๋ฆฌํฉํ ๋ง์ด ๊ฐ๋ฅํ ๊ตฌ์กฐ
- annotation ๊ธฐ๋ฐ์ผ๋ก MVC ํ๋ ์์ํฌ๋ฅผ ๊ตฌํํ ํ ๋ ๊ฑฐ์ MVC ํ๋ ์์ํฌ(8์ฅ์์ ๊ตฌํํ๋ MVC ํ๋ ์์ํฌ)์ annotation ๊ธฐ๋ฐ์ ์๋ก์ด MVC ํ๋ ์์ํฌ๋ฅผ ํตํฉํ๋ ๋ฐฉ์์ผ๋ก ๊ตฌํํด์ผ ํ๋ค.
- ex. ์์ฒญ URL๊ณผ ์ปจํธ๋กค๋ฌ๋ฅผ ๋งคํํ๋ ํด๋์ค๊ฐ ๋ ๊ฑฐ์ MVC ํ๋ ์์ํฌ =
LegacyHandlerMapping, ์๋ก์ด MVC ํ๋ ์์ํฌ =AnnotationHandlerMapping์ด๋ผ๋ฉดDispatcherServlet์์ ์ด ๋์ ํตํฉํด ๋ ํํ์ ์ปจํธ๋กค๋ฌ๊ฐ ๋ชจ๋ ๋์ ๊ฐ๋ฅํ๋๋ก ๊ตฌํํ ์ ์๋ค.
@WebServlet(name = "dispatcher", urlPatterns = "/", loadOnStartup = 1)
public class DispatcherServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final Logger logger = LoggerFactory.getLogger(DispatcherServlet.class);
private LegacyHandlerMapping lhm;
private AnnotationHandlerMapping ahm;
@Override
public void init() throws ServletException {
lhm = new LegacyHandlerMapping();
lhm.initMapping();
ahm = new AnnotationHandlerMapping("next.controller");
ahm.initialize();
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try {
Controller controller = lhm.findController(req.getRequestURI());
if (controller != null) {
render(req,resp,controller.execute(req, resp));
} else {
HandlerExecution he = ahm.getHandler(req);
if (he == null) {
throw new ServletException("์ ํจํ์ง ์์ ์์ฒญ์
๋๋ค.");
}
render(req, resp, he.handle(req, resp));
}
} catch (Throwable e) {
throw new ServletException(e.getMessage());
}
}
private void render (HttpServletRequest req, HttpServletResponse resp, ModelAndView mav) throws Exception {
View view = mav.getView();
view.render(mav.getModel(), req, resp);
}
}
HandlerExecution์ ์๋ก ์ถ๊ฐํ MVC ํ๋ ์์ํฌ์ ์ปจํธ๋กค๋ฌ์ ๊ฐ์ ์ญํ ์ ํ๋ ํด๋์ค์ด๋ค.- ์์ ๊ฐ์ ๊ตฌ์กฐ๋ก ๊ตฌํํ๋ค๋ฉด ์๋ก ๋ค๋ฅธ ์ปจํธ๋กค๋ฌ๋ฅผ ๊ฐ์ด ์๋น์คํ๋ ๊ฒ์ด ๊ฐ๋ฅํ ๊ฒ์ด๋ค.
์ค์ต์ 1์ฐจ ๋ชฉํ๋ "๋ ๊ฑฐ์ MVC ํ๋ ์์ํฌ์ annotation ๊ธฐ๋ฐ์ ์๋ก์ด MVC ํ๋ ์์ํฌ๊ฐ ๋์์ ์๋น์ค ๊ฐ๋ฅํ๋๋ก ๊ตฌํํ๋ ๊ฒ"์ด๋ค.
- 1์ฐจ ๊ตฌํ์ ์๋ฃํ ํ, ๋ฆฌํฉํ ๋งํ ๋ถ๋ถ์ ์ฐพ์๋ณด๋ฉด 2๊ฐ์ ํ๋ ์์ํฌ๊ฐ ๊ณต์กดํ๊ธฐ์ ์ธํฐํ์ด์ค๋ก ์ถ์ํํ๋ฉด ์ข์๋ณด์ด๋ ๋ถ๋ถ์ด ์์ ๊ฒ์ด๋ค.
์ด ์ค์ต์ ํด๋์คํจ์ค๋ก ์ค์ ๋์ด ์๋ ํด๋์ค ์ค
@Controllerannotation์ด ์ค์ ๋์ด ์๋ ํด๋์ค๋ฅผ ์ฐพ๊ธฐ ์ํด reflections ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ํ์ฉํ ์ ์๋ค. ์ด๋ฅผ ํ์ฉํด@Controllerannotation์ด ์ค์ ๋ ํด๋์ค๋ฅผ ์ฐพ์ ํ@RequestMapping์ค์ ์ ๋ฐ๋ผ ์์ฒญ URL๊ณผ ๋ฉ์๋๋ฅผ ์ฐ๊ฒฐํ๋๋ก ๊ตฌํํ ์ ์๋ค.
๐ง ์๋ฐ ๋ฆฌํ๋ ์
๋ฆฌํ๋ ์ ์ด๋ : ๊ตฌ์ฒด์ ์ธ ํด๋์ค ํ์ ์ ์์ง ๋ชปํด์ ๊ทธ ํด๋์ค์ ๋ฉ์๋์ ํ์ ๊ทธ๋ฆฌ๊ณ ๋ณ์๋ค์ ์ ๊ทผํ ์ ์๋๋ก ํด์ฃผ๋ ์๋ฐ API์ด๋ค.
์๋ฐ ๋ฆฌํ๋ ์
API ํ์ฉํด ํด๋์ค ์ ๋ณด ์ถ๋ ฅ
Question ํด๋์ค์ ๋ชจ๋ ํ๋, ์์ฑ์, ๋ฉ์๋ ์ ๋ณด๋ฅผ ์ถ๋ ฅํ๋ค. (core.ref.ReflectionTest ์ showClass() ๋ฉ์๋ ๊ตฌํ)
@Test
public void showClass() {
Class<Question> clazz = Question.class;
logger.debug(clazz.getName());
logger.debug(Arrays.toString(clazz.getDeclaredFields()));
logger.debug(Arrays.toString(clazz.getConstructors()));
logger.debug(Arrays.toString(clazz.getMethods()));
}
Question์ ๋ชจ๋ ํ๋๊ฐprivate์ด๋ฏ๋กgetDeclaredFields()๋ฅผ ์ฌ์ฉํ๊ณ , ์์ฑ์์ ๋ฉ์๋๋ค์public์ด๋ฏ๋กget...()์ ์ฌ์ฉํ๋ค.
"test" ๋ก ์์ํ๋ ๋ฉ์๋ ์คํ
core.ref.Junit3Test ์์ test ๋ก ์์ํ๋ ๋ชจ๋ ๋ฉ์๋๋ฅผ ์คํํด์ผ ํ๋ค. core.ref.Junit3TestRunner ํด๋์ค์ run() ๋ฉ์๋๋ฅผ ์คํํ์ ๋, Junit3Test ์์ ๋ฉ์๋ ์ด๋ฆ์ด test ๋ก ์์ํ๋ ๋ชจ๋ ๋ฉ์๋๋ฅผ ์คํํ๋ฉด ๋๋ค.
public class Junit3TestRunner {
@Test
public void run() throws Exception {
Class<Junit3Test> clazz = Junit3Test.class;
Method[] ary = clazz.getMethods();
for (Method str : ary) {
if (str.getName().startsWith("test")) {
str.invoke(clazz.newInstance());
}
}
}
}
Method๋ฐฐ์ด์Junit3Test.class์ ์๋public๋ฉ์๋๋ค์ ๊ฐ์ ธ์ค๊ณ ,getName()์ผ๋ก ์ด๋ฆ์ ์ป์ดtest๋ก ์์ํ๋์ง ํ์ธํ๊ณ ์คํํ๋ค.
@MyTest annotation์ผ๋ก ์ค์ ๋ ๋ฉ์๋ ์คํ
core.ref.Junit4Test ์์ @MyTest annotation์ด ์ค์ ๋์ด ์๋ ๋ชจ๋ ๋ฉ์๋๋ฅผ ์คํํ๋ค. ์์ ๋น์ทํ ๋ฐฉ์์ด๋ค.
public class Junit4TestRunner {
@Test
public void run() throws Exception {
Class<Junit4Test> clazz = Junit4Test.class;
Method[] ary = clazz.getMethods();
for (Method tmp : ary) {
if (tmp.isAnnotationPresent(MyTest.class)) {
tmp.invoke(clazz.newInstance());
}
}
}
}
isAnnotationPresent๋ฉ์๋๋ก ํด๋นMethod๊ฐ ์ธ์๋ก ๋๊ฒจ์ค annotation์ด ์ค์ ๋์๋์ง ํ์ธํ ์ ์๋ค.
์์ฑ์๊ฐ ์๋ ํด๋์ค์ ์ธ์คํด์ค ์์ฑ
User ํด๋์ค์ ์ธ์คํด์ค๋ฅผ ์์ฑํ๋ค. ํด๋น ํด๋์ค๋ ๊ธฐ๋ณธ ์์ฑ์๊ฐ ์์ผ๋ฉฐ, 4๊ฐ์ ์ธ์๋ฅผ ๊ฐ์ง๋ ์์ฑ์๋ฐ์ ์๋ค. core.ref.ReflectionTest ์ newInstanceWithConstructorArgs() ๋ฉ์๋๋ฅผ ๊ตฌํํ๋ค.
@Test
public void newInstanceWithConstructorArgs() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
Class<User> clazz = User.class;
logger.debug(clazz.getName());
Constructor<?> constructor = clazz.getDeclaredConstructor();
constructor.newInstance();
}
getDeclaredConstructors()๋ฅผ ํตํด ์ธ์คํด์ค ์์ฑ์ ์ํ ์์ฑ์๋ฅผ ๋จผ์ ์ฐพ์ผ๋ผ๋๋ฐ,NoSuchMethodException์์ธ๊ฐ ๋ฐ์ํ๋ค.
private ํ๋์ ์ ๊ทผ
๋ฆฌํ๋ ์
API๋ฅผ ํตํด ์ธ์คํด์ค๋ฅผ ์์ฑํ์ฌ ํด๋์ค์ private ํ๋์ ์ ๊ทผํด ๊ฐ์ ์ ๋ฌํ ์ ์๋ค. ๋ค์๊ณผ ๊ฐ์ด
2๊ฐ์ private ํ๋๋ฅผ ๊ฐ์ง๋ ํด๋์ค๊ฐ ์๋ ๊ฒฝ์ฐ, ์ธ์คํด์ค๋ฅผ ์ง์ ์์ฑํด ๊ฐ์ ์ ๋ฌํ ์ ์๋ค. ์ด๋ ์๋ฐ ๋ฆฌํ๋ ์
API๋ฅผ ํ์ฉํด ๊ฐ์ ๊ฐ์ง๋ ์ธ์คํด์ค๋ฅผ ์์ฑํ ์ ์๋ค.
package next.model;
public class Student {
private String name;
private int age;
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
Studentํด๋์ค์name๊ณผageํ๋์ ๊ฐ์ ํ ๋นํ๊ณ ,getter๋ฉ์๋๋ฅผ ํตํด ๊ฐ์ ํ์ธํ๋ค.
@Test
public void privateFieldAccess() throws NoSuchFieldException, IllegalAccessException {
Class<Student> clazz = Student.class;
logger.debug(clazz.getName());
next.model.Student student = new next.model.Student();
Field[] fields = clazz.getDeclaredField("name");
for (Field field : fields) {
field.setAccessible(true);
...
}
logger.debug(student.getName() + student.getAge());
}
Field์ ๋ณด๋ฅผ ๊ฐ์ ธ์์ ์ ๊ทผ ๊ถํ์true๋ก ๋ฐ๊ฟ์ค ํ ๊ฐ์ ์ธํ ํ๋ค.
๐ง ์๊ตฌ์ฌํญ ๋ถ๋ฆฌ ๋ฐ ํํธ
reflections๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ํ์ฉํด@Controllerannotation์ด ์ค์ ๋์ด ์๋ ๋ชจ๋ ํด๋์ค๋ฅผ ์ฐพ๊ณ , ๊ฐ ํด๋์ค์ ๋ํ ์ธ์คํด์ค ์์ฑ์ ๋ด๋นํ๋ControllerScannerํด๋์ค๋ฅผ ์ถ๊ฐํ๋ค.- annotation ๊ธฐ๋ฐ ๋งคํ์ ๋ด๋นํ
AnnotationHandlerMappingํด๋์ค๋ฅผ ์ถ๊ฐํ ํ ์ด๊ธฐํํ๋ค. AnnotationHandlerMappingํด๋์ค์ ํด๋ผ์ด์ธํธ ์์ฒญ ์ ๋ณด(HttpServletRequest)๋ฅผ ์ ๋ฌํ๋ฉด ์์ฒญ์ ํด๋นํ๋HandlerExecution์ ๋ฐํํ๋ ๋ฉ์๋๋ฅผ ๊ตฌํํ๋ค.
annotation ๊ธฐ๋ฐ์ผ๋ก ๋์ํ๋ ์๋ก์ด ๋งคํ ํด๋์ค(AnnotationHandlerMapping) ์ถ๊ฐ๋ฅผ ์๋ฃํ๋ค. ๋ค์์ ๊ธฐ์กด์ ์ฌ์ฉํ๋ MVC ํ๋ ์์ํฌ์ RequestMapping ๊ณผ ์๋ก ์ถ๊ฐํ AnnotationHandlerMapping ์ด ๊ฐ์ด ๋์ํ๋๋ก ๊ตฌํํ๋ ๋จ๊ณ์ด๋ค. ์ด ๊ณผ์ ์ผ๋ก RequestMapping , AnnotationHandlerMapping ์ ํตํฉํ ์ ์๋ค๋ฉด, ๊ธฐ์กด์ ๊ตฌํํ ์ปจํธ๋กค๋ฌ์ ์๋ก ์ถ๊ฐํ ์ปจํธ๋กค๋ฌ(@Controller)๋ฅผ ๊ฐ์ด ์ฌ์ฉํ ์ ์๊ฒ ๋ค.
RequestMapping,AnnotationHandlerMapping์ ์์ฒญ URL๊ณผ ์คํํ ์ปจํธ๋กค๋ฌ ํด๋์ค ๋๋ ๋ฉ์๋๋ฅผ ๋งคํํ๋ ์ญํ ์ ๊ฐ๋ค. ํ์ง๋ง ์๋ ๋งคํ์ด๋, annotation ๊ธฐ๋ฐ ์๋ ๋งคํ์ด๋ ์ฐจ์ด์ด๋ค. ๋ ํด๋์ค์ ์ค๋ณต๋ ๋ถ๋ถ์ ์ธํฐํ์ด์ค๋ก ์ถ์ํํ๋ค. (HandlerMappinginterface)DispatcherServlet์ ์ด๊ธฐํ(init()๋ฉ์๋) ๊ณผ์ ์์LegacyHandlerMapping,AnnotationHandlerMapping๋ชจ๋ ์ด๊ธฐํํ๋ค. ๋HandlerMapping์List๋ก ๊ด๋ฆฌํ๋ค.DispatcherServlet์service()๋ฉ์๋์์๋ ์์์ ์ด๊ธฐํํ 2๊ฐ์HandlerMapping์์ ์์ฒญ URL์ ํด๋นํ๋ ์ปจํธ๋กค๋ฌ๋ฅผ ์ฐพ์ ๋ฉ์๋๋ฅผ ์คํํ๋ค.- ๊ธฐ์กด ์ปจํธ๋กค๋ฌ๋ฅผ ์๋ก ์ถ๊ฐํ annotation ๊ธฐ๋ฐ์ผ๋ก ์ค์ ํ๊ณ ํ ์คํธํ๋ค.
๐ฉ MVC ํ๋ ์์ํฌ ๊ตฌํ 3๋จ๊ณ
๐ง @Controller annotation ์ค์ ํด๋์ค ์ค์บ
annotation ๊ธฐ๋ฐ์ผ๋ก MVC ํ๋ ์์ํฌ๋ฅผ ๊ตฌํํ๋ ค๋ฉด ๋จผ์ @Controller annotation์ด ์ค์ ๋ ํด๋์ค๋ฅผ ์ฐพ์์ผ ํ๋ค. ๋ฆฌํ๋ ์
์ ์ด์ฉํด ํด๋น ํด๋์ค๋ค์ ์ฐพ๊ณ , ๊ฐ ํด๋์ค์ ๋ํ ์ธ์คํด์ค ๋ชฉ๋ก๊น์ง ๊ตฌํํ๋ค.
public class ControllerScanner {
private static final Logger log = LoggerFactory.getLogger(ControllerScanner.class);
private Reflections reflections;
public ControllerScanner(Object... backPackage) {
reflections = new Reflections(backPackage);
}
public Map<Class<?>, Object> getControllers() {
Set<Class<?>> preInitiatedControllers = reflections.getTypesAnnotatedWith(Controller.class);
return instantiateControllers(preInitiatedControllers);
}
Map<Class<?>, Object> instantiateControllers(Set<Class<?>> preInitiatedControllers) {
Map<Class<?>, Object> controllers = Maps.newHashMap();
try {
for (Class<?> clazz : preInitiatedControllers) {
controllers.put(clazz, clazz.newInstance());
}
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
return controllers;
}
}
- ํ
์คํธ๋ฅผ ์ํด
@Controllerannotation์ ์ฌ์ฉํ๋ ํ ์คํธ์ฉ ์ปจํธ๋กค๋ฌ๋ฅผ ๊ตฌํํ๊ณ , ํ ์คํธ ์ฝ๋๋ฅผ ์์ฑํ๋ค.
@Controller
public class MyController {
private static final Logger log = LoggerFactory.getLogger(MyController.class);
@RequestMapping("/users/findUserId")
public ModelAndView list(HttpServletRequest req, HttpServletResponse res) {
log.debug("users findUserId");
return new ModelAndView(new JspView("/users/list.jsp"));
}
@RequestMapping(value = "/users/show", method = RequestMethod.GET)
public ModelAndView show(HttpServletRequest req, HttpServletResponse res) {
log.debug("users findUserId");
return new ModelAndView(new JspView("/users/show.jsp"));
}
@RequestMapping(value = "/users", method = RequestMethod.POST)
public ModelAndView create(HttpServletRequest req, HttpServletResponse res) {
log.debug("users create");
return new ModelAndView(new JspView("redirect:/users"));
}
}
public class ControllerScannerTest {
private static final Logger log = LoggerFactory.getLogger(ControllerScannerTest.class);
private ControllerScanner cs;
@Before
public void setup() {
cs = new ControllerScanner("core.nmvc");
}
@Test
public void getControllers() throws Exception {
Map<Class<?>, Object> controllers = cs.getControllers();
for (Class<?> controller : controllers.keySet()) {
log.debug("controller : {}", controller);
}
}
}
-
ํ ์คํธ์์๋ ์ปจํธ๋กค๋ฌ๋ฅผ ์ ๋งคํํ์ฌ ์ ๋ณด๋ฅผ ๊ฐ์ ธ์ค๋์ง ์ถ๋ ฅ๋ฌธ์ผ๋ก ํ์ธํ๋ค.
- ์์์
@Controllerannotation์ ์ฌ์ฉํMyController์ปจํธ๋กค๋ฌ๋ฅผ ์ถ๋ ฅํ๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
- ์์์
๐ง @RequestMapping annotation ์ค์ ์ ํ์ฉํ ๋งคํ
์์์ ์ฐพ์ ์ปจํธ๋กค๋ฌ ํด๋์ค์ RequestMapping annotation ์ค์ ์ ๊ธฐ๋ฐ์ผ๋ก ๋งคํํ๋ค. ๋งคํ์ ์ด์ MVC ํ๋ ์์ํฌ์ ๊ฐ์ด Map ์ ํ์ฉํ๋ค.
- ๋ค๋ฅธ ์ ์
Map์ key๋ก ์ฌ์ฉ๋๋ ๊ฐ์ด ์์ฒญ URL๊ณผ HTTP ๋ฉ์๋์ ์กฐํฉ์ด๋ผ๋ ์ ์ด๋ค. - ์์ฒญ URL๊ณผ HTTP ๋ฉ์๋ ์ ๋ณด๋ฅผ ๊ฐ์ง๋ ํด๋์ค๋ฅผ
HandleKey๋ผ๋ ์ด๋ฆ์ผ๋ก ๊ตฌํํ๋ค.
public class HandlerKey {
private String url;
private RequestMethod requestMethod;
public HandlerKey(String url, RequestMethod requestMethod) {
this.url = url;
this.requestMethod = requestMethod;
}
@Override
public String toString() {
return "HandlerKey [url=" + url + ", requestMethod=" + requestMethod + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((requestMethod == null) ? 0 : requestMethod.hashCode());
result = prime * result + ((url == null) ? 0 : url.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
HandlerKey other = (HandlerKey) obj;
if (requestMethod != other.requestMethod)
return false;
if (url == null) {
if (other.url != null)
return false;
} else if (!url.equals(other.url))
return false;
return true;
}
}
HashMap์ ํค๋ก ํ์ฉํ๊ธฐ ์ํดhashCode(),equals()๋ฉ์๋๋ฅผ ๋ฐ๋์ ๊ตฌํํ๋ค.-
Map์ ๊ฐ์@RequestMappingannotation์ด ์ค์ ๋์ด ์๋ ๋ฉ์๋ ์ ๋ณด์ด๋ค.- ๊ฐ์ ์ ์ฅ๋๋ ๋ฉ์๋ ์ ๋ณด๋ ์๋ฐ ๋ฆฌํ๋ ์ ์ผ๋ก ํด๋น ๋ฉ์๋๋ฅผ ์คํํ ์ ์๋ ์ ๋ณด์ฌ์ผ ํ๋ค.
- ์ฆ, ๋ฉ์๋๊ฐ ์์นํ๋ ํด๋์ค์ ์ธ์คํด์ค ์ ๋ณด์
java.lang.reflect.Method์ ๋ณด๋ฅผ ๊ฐ์ ธ์ผ ํ๋ค. - ์ด๋ฅผ
HandlerExecutionํด๋์ค๋ก ๊ตฌํํ๋ค.
public class HandlerExecution {
private static final Logger log = LoggerFactory.getLogger(HandlerExecution.class);
private Object declaredObject;
private Method method;
public HandlerExecution(Object declaredObject, Method method) {
this.declaredObject = declaredObject;
this.method = method;
}
public ModelAndView handle(HttpServletRequest request, HttpServletResponse response) throws Exception {
try {
return (ModelAndView) method.invoke(declaredObject, request, response);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
log.error("{} method invoke fail. error message : {}", method, e.getMessage());
throw new RuntimeException(e);
}
}
}
์ด์ HandleKey ์ HandleExecution ์ ์ฐ๊ฒฐ(mapping)ํ๋๋ก ํ๋ค. ๋งคํ ์ด๊ธฐํ ์์
์ AnnotationHandlerMapping ํด๋์ค์์ ๊ตฌํํ๋ค.
public class AnnotationHandlerMapping {
private static final Logger log = LoggerFactory.getLogger(AnnotationHandlerMapping.class);
private Object[] basePackage;
private Map<HandlerKey, HandlerExecution> handlerExecutions = Maps.newHashMap();
public AnnotationHandlerMapping(Object... basePackage) {
this.basePackage = basePackage;
}
public void initialize() {
ControllerScanner controllerScanner = new ControllerScanner(basePackage);
Map<Class<?>, Object> controllers = controllerScanner.getControllers();
Set<Method> methods = getRequestMappingMethods(controllers.keySet());
for (Method method : methods) {
RequestMapping rm = method.getAnnotation(RequestMapping.class);
log.debug("register handlerExecution : url is {}, method is {}", rm.value(), method);
handlerExecutions.put(createHandleKey(rm), new HandlerExecution(controllers.get(method.getDeclaringClass()), method));
}
}
private HandlerKey createHandleKey(RequestMapping rm) {
return new HandlerKey(rm.value(), rm.method());
}
private Set<Method> getRequestMappingMethods(Set<Class<?>> controllers) {
Set<Method> requestMappingMethods = Sets.newHashSet();
for (Class<?> clazz : controllers) {
requestMappingMethods.addAll(ReflectionUtils.getAllMethods(clazz, ReflectionUtils.withAnnotation(RequestMapping.class)));
}
return requestMappingMethods;
}
...
}
๐ง ํด๋ผ์ด์ธํธ ์์ฒญ์ ํด๋นํ๋ HandlerExecution ๋ฐํ
๋ง์ง๋ง์ผ๋ก ํด๋ผ์ด์ธํธ ์์ฒญ์ ํด๋นํ๋ HandlerExecution ์ ์กฐํํ๋ ๋ฉ์๋์ด๋ค. AnnotationHandlerMapping ์ ๋ค์๊ณผ ๊ฐ์ด ๊ตฌํํ๋ค.
public class AnnotationHandlerMapping {
private static final Logger log = LoggerFactory.getLogger(AnnotationHandlerMapping.class);
private Object[] basePackage;
private Map<HandlerKey, HandlerExecution> handlerExecutions = Maps.newHashMap();
...
public HandlerExecution getHandler(HttpServletRequest request) {
String requestUri = request.getRequestURI();
RequestMethod rm = RequestMethod.valueOf(request.getMethod().toUpperCase());
log.debug("requestUri : {}, requestMethod : {}", requestUri, rm);
return handlerExecutions.get(new HandlerKey(requestUri, rm));
}
}
AnnotationHandlerMappingTest ๋ก ์ ์์ ์ผ๋ก ๊ตฌํ์ ์๋ฃํ๋์ง ํ๋จํ๋ค.
๐ง DispatcherServlet๊ณผ AnnotationHandlerMapping ํตํฉ
์ด์ DispatcherServlet ์์ ์๋ก ์ถ๊ฐํ AnnotationHandlerMapping ์ ํ์ฉํด ์๋น์คํ ์ ์๋๋ก ํตํฉํ๋ ์์
์ ์ํํ ์ฐจ๋ก์ด๋ค.
-
DispatcherServlet์RequestMapping๊ณผAnnotationHandlerMapping์ ๋ชจ๋ ์ง์ํด์ผ ํ๋ค.- ๋ถ๋ฆฌํด์ ๊ด๋ฆฌํ ์๋ ์์ง๋ง ๋์ ์ผ๊ด๋ ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ๋๋ก ํตํฉํ ํ ๊ด๋ฆฌํ๋ ๊ฒ์ด ํฅํ ํ์ฅ์ฑ์ ๊ณ ๋ คํ์ ๋ ์ ์ ํ๋ค๊ณ ํ๋จ๋๋ค.
- ๋จผ์ ๋
Mappingํด๋์ค์ ๋ํด ์ถ์ํํ ์ธํฐํ์ด์ค๋ฅผHandlerMapping์ด๋ผ๋ ์ด๋ฆ์ผ๋ก ์ถ๊ฐํ๋ค.
public interface HandlerMapping {
Object getHandler(HttpServletRequest request);
}
์ด์ ์ ์ธํฐํ์ด์ค๋ฅผ ๋ ํด๋์ค๊ฐ ๊ตฌํํ๋๋ก ํ๋ค. RequestMapping ํด๋์ค๋ฅผ LegacyHandlerMapping ์ผ๋ก ๋ณ๊ฒฝํ์ฌ ๊ตฌํํ๋ค.
public class LegacyHandlerMapping implements HandlerMapping {
private static final Logger logger = LoggerFactory.getLogger(DispatcherServlet.class);
private Map<String, Controller> mappings = new HashMap<>();
public void initMapping() {
...
}
...
@Override
public Object getHandler(HttpServletRequest request) {
return mappings.get(request.getRequestURI());
}
}
- ์์ฒญ์ URI๋ฅผ ๊ฐ์ ธ์
mappings์์ ์กฐํํด ๋ฐํํ๋ค.
public class AnnotationHandlerMapping implements HandlerMapping {
...
public HandlerExecution getHandler(HttpServletRequest request) {
String requestUri = request.getRequestURI();
RequestMethod rm = RequestMethod.valueOf(request.getMethod().toUpperCase());
log.debug("requestUri : {}, requestMethod : {}", requestUri, rm);
return handlerExecutions.get(new HandlerKey(requestUri, rm));
}
}
AnnotationHandlerMapping์ ์ด๋ฏธgetHandler๋ฅผ ๊ตฌํํ๊ธฐ์ ์ถ๊ฐ ๊ตฌํ์ ์๋ค.
์ด์ DispatcherServlet ์ด ๋ ๊ฐ์ HandlerMapping ์ด ๋ชจ๋ ๋์ํ๋๋ก ํตํฉํ๋ ์์
์ ์ํํ๋ค.
- ์ด๊ธฐํ๊ฐ ๋๋
HandlerMapping์List๋ก ๊ด๋ฆฌํ๋ค. - ์์ฒญ URL๊ณผ HTTP ๋ฉ์๋์ ํด๋นํ๋ ์ปจํธ๋กค๋ฌ๋ฅผ ์ฐพ์ ์์ ์ ์ํํ๋ค.
@WebServlet(name = "dispatcher", urlPatterns = "/", loadOnStartup = 1)
public class DispatcherServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final Logger log = LoggerFactory.getLogger(DispatcherServlet.class);
private List<HandlerMapping> mappings = Lists.newArrayList();
private LegacyHandlerMapping lhm;
private AnnotationHandlerMapping ahm;
@Override
public void init() throws ServletException {
lhm = new LegacyHandlerMapping();
lhm.initMapping();
ahm = new AnnotationHandlerMapping("next.controller");
ahm.initialize();
mappings.add(lhm);
mappings.add(ahm);
}
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Object handler = getHandler(req);
if (handler == null) {
throw new IllegalArgumentException("์กด์ฌํ์ง ์๋ URL์
๋๋ค.");
}
try {
ModelAndView mav = execute(handler, req, resp);
View view = mav.getView();
view.render(mav.getModel(), req, resp);
} catch (Throwable e) {
log.error("Exception : {}", e);
throw new ServletException(e.getMessage());
}
}
private Object getHandler(HttpServletRequest req) {
for (HandlerMapping handlerMapping : mappings) {
Object handler = handlerMapping.getHandler(req);
if (handler != null) {
return handler;
}
}
return null;
}
private ModelAndView execute(Object handler, HttpServletRequest req, HttpServletResponse resp) throws Exception {
if (handler instanceof Controller) {
return ((Controller)handler).execute(req, resp);
} else {
return ((HandlerExecution)handler).handle(req, resp);
}
}
}
-
ํตํฉ์ ์๋ฃํ ํ ๊ธฐ์กด์ ๊ตฌํํ ์ปจํธ๋กค๋ฌ๋ฅผ ์๋กญ๊ฒ ์ถ๊ฐํ ์ปจํธ๋กค๋ฌ ๊ธฐ๋ฐ์ผ๋ก ๋ณ๊ฒฝํ๋ค.
- ๊ธฐ์กด ์ปจํธ๋กค๋ฌ์ ๋ํ ์ง์๋ ๊ฐ๋ฅํ๋ฉด์ ์๋ก ์ถ๊ฐํ
AnnotationHandlerMapping์ง์๋ ๊ฐ๋ฅํด ์ ์ง์ ์ธ ๋ฆฌํฉํ ๋ง ์์ ์ด ๊ฐ๋ฅํ๋ค.
- ๊ธฐ์กด ์ปจํธ๋กค๋ฌ์ ๋ํ ์ง์๋ ๊ฐ๋ฅํ๋ฉด์ ์๋ก ์ถ๊ฐํ
- ์ฌ์ฉ์ ๊ด๋ฆฌ ๊ธฐ๋ฅ์ ์ผ๋ถ๋ฅผ
UserController๋ฅผ ์ถ๊ฐํด ์ด์ ์์ ํ์ฌ ์ ์ ๋์์ ํ์ธํ๋ค.
public class UserController extends AbstractNewController {
private static final Logger log = LoggerFactory.getLogger(UserController.class);
private UserDao userDao = UserDao.getInstance();
@RequestMapping("/users")
public ModelAndView list(HttpServletRequest req, HttpServletResponse resp) throws SQLException {
if (!UserSessionUtils.isLogined(req.getSession())) {
return jspView("redirect:/users/loginForm");
}
ModelAndView mav = jspView("/user/list.jsp");
mav.addObject("users", userDao.findAll());
return mav;
}
@RequestMapping("/users/form")
public ModelAndView form(HttpServletRequest req, HttpServletResponse resp) {
return jspView("/user/form.jsp");
}
@RequestMapping(value = "/users/create", method = RequestMethod.POST)
public ModelAndView create(HttpServletRequest req, HttpServletResponse resp) {
User user = new User(
req.getParameter("userId"),
req.getParameter("password"),
req.getParameter("name"),
req.getParameter("email")
);
log.debug("User : {}", user);
userDao.insert(user);
return jspView("redirect:/");
}
}
์ด์ ๊ฐ์ด ์ปจํธ๋กค๋ฌ ํด๋์ค ํ๋๋ก ํตํฉ ์์ ์ ์งํํ์ฌ
- ๊ด๋ฆฌํ ํด๋์ค ์๋ ์ ์ด์ง๊ณ ,
- ์ปจํธ๋กค๋ฌ์์ ๋ฐ์ํ๋ ์ค๋ณต๋ ์ ๊ฑฐํ ์ ์๋ค.
- ๋ํ ์ ์ง์ ์ธ ์ด์ ์์ ์ผ๋ก ์๋น์ค ์ค๋จ์ด ์๋ค.
๐ฉ ์ธํฐํ์ด์ค๊ฐ ๋ค๋ฅธ ๊ฒฝ์ฐ ํ์ฅ์ฑ ์๋ ์ค๊ณ
๊ธฐ์กด ์ปจํธ๋กค๋ฌ์ ์๋ก ์ถ๊ฐํ ์ปจํธ๋กค๋ฌ ํด๋์ค๋ฅผ ๋ณด๋ฉด, ๋ฉ์๋ ์ธ์์ ๋ฐํ ๊ฐ์ด ๊ฐ์ ํตํฉ์ด ๊ฐ๋ฅํ๋ค. HandlerExecution ํด๋์ค๊ฐ Controller ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ๋๋ก ๋ฆฌํฉํ ๋งํ์ฌ HandlerMapping ์ getHandler() ๋ฉ์๋์ ๋ฐํ ๊ฐ๋ Object ๊ฐ ์๋ Controller ๋ก ๋ณ๊ฒฝํ ์ ์๋ค.
์ด๋ก์จ, ์บ์คํ ๊ณผ ๊ฐ์ ์์ ์ด ํ์์์ด ์์ค์ฝ๋๊ฐ ๊น๋ํด์ง์ง๋ง, ํ์ฅ์ฑ์ด ๋จ์ด์ง๋ค๋ ๋จ์ ์ด ๋ฐ์ํ๋ค.
- ๋ง์ฝ ๋ชจ๋ ์ฝ๋๋ฅผ ํ๋ก์ ํธ ๋ด์์ ์์ ํ ์ ์๋ ๊ถํ์ด ์๋ค๋ฉด ํ๋๋ก ํตํฉํด ๊ด๋ฆฌํ๋ ๊ฒ์ด ๊ฐ๋ฅํ๋ค.
- ํ์ง๋ง ์ธ๋ถ ๋ผ์ด๋ธ๋ฌ๋ฆฌ, ํ๋ ์์ํฌ๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์ปจํธ๋กค๋ฌ๋ฅผ ๊ฐ๋ฐํ๋ค๋ฉด ์ด ์ปจํธ๋กค๋ฌ์ ์ธํฐํ์ด์ค๋ฅผ ํ๋๋ก ํตํฉํ๋ ๊ฒ์ ๋ถ๊ฐ๋ฅํ๋ค.
"์ ํ๋ฆฌ์ผ์ด์ ์ ๊ฐ๋ฐํ๋ค๋ณด๋ฉด, ์ญํ ์ ๊ฐ์๋ฐ ์๋ก ๋ค๋ฅธ ์ธํฐํ์ด์ค๋ฅผ ์ฌ์ฉํ์ฌ ํตํฉํ๊ธฐ ํ๋ ์ํฉ์ด ๋ฐ์ํ๋ค. ๋ฐ๋ผ์ ๋ ์ ์ฐํ ๊ตฌ์กฐ๋ฅผ ์ง์ํ๋ ค๋ฉด ์ธํฐํ์ด์ค ํ๋๋ก ๊ฐ์ ํ๋ ๊ฒ์ ๋ฐ๋์งํ์ง ์๋ค."
์๋ก ๋ค๋ฅธ ์ธํฐํ์ด์ค ํตํฉ ์, DispatcherServlet ์ execute() ๋ฉ์๋์ ๊ตฌํ ์ฝ๋์ ๊ฐ์ด ์บ์คํ
์ ํด์ผ ํ๋ ์ํฉ์ด ์ข
์ข
๋ฐ์ํ๋ค.
public class DispatcherServlet extends HttpServlet {
...
private ModelAndView execute(Object handler, HttpServletRequest req, HttpServletResponse resp) throws Exception {
if (handler instanceof Controller) {
return ((Controller)handler).execute(req, resp);
} else {
return ((HandlerExecution)handler).handle(req, resp);
}
}
}
์ ๊ตฌ์กฐ๋ ์๋ก์ด ์ปจํธ๋กค๋ฌ ์ ํ์ด ์ถ๊ฐ๋ ๊ฒฝ์ฐ, else if ์ ์ด ์ถ๊ฐ๋๋ ๊ตฌ์กฐ์ด๋ค. ์ด๋ฅผ ๊ฐ์ ํด ์๋ก ๊ฐ์ ์ํฅ์ ์ฃผ์ง ์์ผ๋ฉด์ ํ์ฅํ ์ ์๋ ๋ฐฉ๋ฒ์ ๊ฐ๊ตฌํ๋ค. (๊ฐ๋ฐฉ ํ์์ ์์น์ ์ค์?)
-
์๋ก ๋ค๋ฅธ ์ธํฐํ์ด์ค๋ฅผ ์ธํฐํ์ด์ค ํ๋๋ก ์ฐ๊ฒฐํ๋ ๋ฐฉ๋ฒ์ด ํ์ํ๋ค.
Controller์ธํฐํ์ด์ค๋ฅผ ์ถ๊ฐํ ๋๋ ๊ฐ์ ์ ๋ตController๋ ํ๋ ์์ํฌ ๋ด์์ ๋ชจ๋ ์ปจํธ๋กค๋ฌ๋ฅผ ์ถ์ํํ ์ธํฐํ์ด์ค๋ผ๋ฉด, ์ง๊ธ์ ์ฌ๋ฌ ๊ฐ์ ํ๋ ์์ํฌ ์ปจํธ๋กค๋ฌ๋ฅผ ํ๋๋ก ํตํฉํด์ผ ํ๋ ์ด์์ด๋ค.- ๊ฐ ์ปจํธ๋กค๋ฌ์ ์ญํ ์ ๋์ผํ๊ธฐ์, ๋ ํ๋์ ์ถ์ํ ๋จ๊ณ๊ฐ ํ์ํ๋ค๋ ์๋ฏธ์ด๋ค.
DispatcherServlet ์ execute() ๋ฉ์๋์ ๊ตฌํ ๋ก์ง์ ์ปจํธ๋กค๋ฌ์ ์ธ์คํด์ค๊ฐ ๋ฌด์์ธ์ง๋ฅผ ํ๋จํ๋ ๋ถ๋ถ๊ณผ ํด๋น ์ปจํธ๋กค๋ฌ๋ก ์บ์คํ
ํ ํ ์ปจํธ๋กค๋ฌ๋ฅผ ์คํํ๋ ๋ถ๋ถ์ผ๋ก ๋๋๋ค.
- ์ด ๋ถ๋ถ๋ค์
HandlerAdapter์ธํฐํ์ด์ค๋ก ์ถ์ํํ ํ ๊ฐ ์ปจํธ๋กค๋ฌ ๊ตฌํ์ฒด๊ฐDispatcherServlet์execute()๋ก์ง์ ๋๋ ๊ตฌํํ๋ค.
public interface HandlerAdapter {
boolean supports(Object handler);
ModelAndView handle(HttpServletRequest req, HttpServletResponse resp, Object handler) throws Exception;
}
์ด์ ๊ฐ ์ปจํธ๋กค๋ฌ์ ๋ํ HandlerAdapter ๋ฅผ ๊ตฌํํ๋ค. Controller ์ HandlerExecution ์ธํฐํ์ด์ค์ ๋ํ ๊ตฌํ์ฒด์ด๋ค.
public class ControllerHandlerAdapter implements HandlerAdapter {
@Override
public boolean supports(Object handler) {
return handler instanceof Controller;
}
@Override
public ModelAndView handle(HttpServletRequest req, HttpServletResponse resp, Object handler) throws Exception {
return ((Controller)handler).execute(req, resp);
}
}
public class HandlerExecutionHandlerAdapter implements HandlerAdapter {
@Override
public boolean supports(Object handler) {
return handler instanceof Controller;
}
@Override
public ModelAndView handle(HttpServletRequest req, HttpServletResponse resp, Object handler) throws Exception {
return ((HandlerExecution)handler).handle(req, resp);
}
}
์ด์ DispatcherServlet ์ ๋ค์๊ณผ ๊ฐ์ด ๋ฆฌํฉํ ๋งํ๋ค.
@WebServlet(name = "dispatcher", urlPatterns = "/", loadOnStartup = 1)
public class DispatcherServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final Logger log = LoggerFactory.getLogger(DispatcherServlet.class);
private List<HandlerMapping> mappings = Lists.newArrayList();
private List<HandlerAdapter> handlerAdapters = Lists.newArrayList();
private LegacyHandlerMapping lhm;
private AnnotationHandlerMapping ahm;
@Override
public void init() throws ServletException {
lhm = new LegacyHandlerMapping();
lhm.initMapping();
ahm = new AnnotationHandlerMapping("next.controller");
ahm.initialize();
mappings.add(lhm);
mappings.add(ahm);
handlerAdapters.add(new ControllerHandlerAdapter());
handlerAdapters.add(new HandlerExecutionHandlerAdapter());
}
...
private ModelAndView execute(Object handler, HttpServletRequest req, HttpServletResponse resp) throws Exception {
for (HandlerAdapter handlerAdapter : handlerAdapters) {
if (handlerAdapter.supports(handler)) {
return handlerAdapter.handle(req, resp, handler);
}
}
return null;
}
}
- ์ด์ ์๋ก์ด ์ปจํธ๋กค๋ฌ ์ ํ์ด ์ถ๊ฐ๋๋๋ผ๋,
HandlerAdapter๊ตฌํ์ฒด๋ง ๊ตฌํ(๋ฐํํ์ ์ ์ปจํธ๋กค๋ฌ์ ์ ํ์ผ๋ก ์์ ) ํDispatcherServlet์HandlerAdapter๋ชฉ๋ก์ ์ถ๊ฐํ๋ฉด ๋๋ค.
์ด ๊ฐ์ ๊ตฌ์กฐ๋ก ํ์ฅํ์ฌ ๋ค๋ฅธ HandlerAdapter ์ ์ํฅ์ ๋ฏธ์น์ง ์์ผ๋ฉด์ ๋
๋ฆฝ์ ์ธ ํ์ฅ์ด ๊ฐ๋ฅํด์ก๋ค.
๐ ์ถ์ฒ
- ์๋ฐ ์น ํ๋ก๊ทธ๋๋ฐ Next Step : ํ๋์ฉ ๋ฒ๊ฒจ๊ฐ๋ ์ํ๊ป์ง ํ์ต๋ฒ - ๋ฐ์ฌ์ฑ
- ์๋ฐ ๋ฆฌํ๋ ์ ๊ฐ๋ ๋ฐ ์ฌ์ฉ๋ฒ ์์๋ณด๊ธฐ